package in.muton.edushare.modules.navigatedWizard;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 * <p>Holding Panel for all the Tabs (which, again, are panels)</p>
 * <p>Must be used in conjunction with AbstarctNavigationTab</p>
 * 
 * @author Naishe
 * @version 0.0.1
 * 
 */
public class NavigatedWizardPanel extends Panel {

	private static Log log = LogFactory.getLog(NavigatedWizardPanel.class);
	private static final long serialVersionUID = 3321739419256930128L;
	private List<AbstractNavigationTab> tabs;
	private int currentTabIndex;
	private AbstractNavigationTab currentTab;
	
	public NavigatedWizardPanel(String id, List<AbstractNavigationTab> tabs) 
	throws Exception {
		super(id);
		this.tabs = tabs;
		this.currentTabIndex = 0;
		
		if(tabs==null || tabs.size()<1)
			throw new Exception("Tab list cannot be NULL or Empty");
		
		currentTab = tabs.get(0);
		
		for(AbstractNavigationTab tab: tabs)
			tab.setNavigatedWizardPanel(this);
		
		//Add stuffs
		ListView<AbstractNavigationTab> tabTitleListView = (new ListView<AbstractNavigationTab>("tablist",tabs) {
			
			private static final long serialVersionUID = 7429727567169353259L;

			@Override
			protected void populateItem(ListItem<AbstractNavigationTab> item) {
				AbstractNavigationTab tab = item.getModelObject();
				item.add(new Label("tabitem",tab.getTabTitle()));
				if(tab == currentTab)
					item.add(new SimpleAttributeModifier("class", "li-selected"));
			}
		});
		
		add(tabTitleListView);
		add(currentTab);
		
	}
	
	public void previous(){
		log.debug("PREVIOUS!");
		if(currentTabIndex > 0){
			currentTab.replaceWith(tabs.get(--currentTabIndex));
		}
		this.currentTab = tabs.get(currentTabIndex);
	}
	
	public void skip(){
		log.debug("SKIP!");
		if(currentTabIndex < tabs.size()-1){
			currentTab.replaceWith(tabs.get(++currentTabIndex));
		}
		this.currentTab = tabs.get(currentTabIndex);
	}
	
	public void next(){
		log.debug("NEXT!");
		currentTab.save();
		if(currentTabIndex < tabs.size()-1){
			currentTab.replaceWith(tabs.get(++currentTabIndex));
		}
		this.currentTab = tabs.get(currentTabIndex);
	}

}
